var app=getApp();
var fakedata=require("../../static/js/fakedata");
Page({
    data: {
        address:'',
        address_name:'',
        longitude: '',
        latitude: '',
        name:'',
        price:'',
        description:'',
        category:'digital',
        userid:'', 
        nickname:'',

        //下面是图片上传有关的变量
        coverFilePath:'',
        descFilePath:'',
        coverUrl:'',
        descUrl:'',
        items: [
            {
                value: 'digital',
                text: '数码产品',
                checked: true
            },
            {
                value: 'appliances',
                text: '家用电器',
                checked: false
            },
            {
                value: 'oldbook',
                text: '二手图书',
                checked: false
            },
            {
                value: 'clothes',
                text: '服饰鞋帽',
                checked: false
            },
            {
                value: 'sports',
                text: '运动户外',
                checked: false
            },
            {
                value: 'lifegoods',
                text: '生活百货',
                checked: false
            },
            {
                value: 'artifact',
                text: '宿舍神器',
                checked: false
            },
        ]
    },
    // 选择地址功能
    chooseLocation() {
        swan.authorize({
            scope: 'scope.userLocation',
            success: res => {
                console.log('authorize', res);
            },
            fail: err => {
                swan.openSetting({});
            }
        });
        swan.chooseLocation({
            success: res => {
                console.log('chooseLocation success', res);
                let longitude = res.longitude;
                let latitude = res.latitude;
                this.setData({
                    address_name: res.name,
                    address: res.address,
                    longitude: longitude,
                    latitude: latitude
                });
            },
            fail: err => {
                console.log('错误码：' + err.errCode);
                console.log('错误信息：' + err.errMsg);
            }
        });
    },
    formatLocation(data) {
        return data.toFixed(2).replace('.', '°');
    },

    // 下面是图片上传的相关功能
    /**
     * 选择图片的功能
     */
    chooseDescImage() {
        swan.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album'],
            success: res => {
                this.setData('descFilePath', res.tempFilePaths[0]);
            }
        });
    },
    chooseCoverImage() {
        swan.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album'],
            success: res => {
                this.setData('coverFilePath', res.tempFilePaths[0]);
            }
        });
    },
    // 上传文件功能
    uploadCoverPic(){
        const coverFilePath = this.getData('coverFilePath');
        if (coverFilePath=='') {
            swan.showToast({
                title: '请先上传图片',
                icon: 'none'
            });
            return false
        }else{
            this.uploadFile(coverFilePath,"coverUrl")
        }
    },
    uploadDescPic(){
        const descFilePath = this.getData('descFilePath');
        if (descFilePath=='') {
            swan.showToast({
                title: '请先上传图片',
                icon: 'none'
            });
            return false
        }
        else{
            this.uploadFile(descFilePath,"descUrl")
        }
    },
    uploadFile(filePath,item) {
        swan.uploadFile({
            url: fakedata.urls.upload,
            filePath,
            method:"POST",
            name: 'file',
            header: {
                'content-type': 'multipart/form-data'
            },
            formData: {
                "file":filePath
            },
            success: res => {
                if(res.statusCode==200){
                    swan.showToast({
                        title: '上传成功',
                        icon: 'none'
                    });
                    console.log('uploadFile success', res);
                    let data=res.data.data;
                    if(item=="descUrl"){
                        this.setData({
                            descUrl:data
                        });
                    }else{
                        this.setData({
                            coverUrl:data
                        });
                    }
                }
            },
            fail: err => {
                console.log('uploadFile fail', err);
                swan.showToast({
                    title: '上传失败',
                    icon: 'none'
                });
            }
        });
    },
    onLoad(e){
        if(app.globalData.islogin){
            this.setData({
                userid:app.globalData.userinfo.id
            });
        }
    },
    onShow(){
        if(!app.globalData.islogin){
            swan.navigateTo({
                url:'/pages/login/login'
            });
            swan.showModal({
                title:'您还没有登录'
            });
        }
    },
    getname(e){
        this.setData({
            name:e.detail.value
        });
    },
    price(e){
        this.setData({
            price:e.detail.value
        })
    },
    description(e){
        this.setData({
            description:e.detail.value
        })
    },
    radioChange(e){
        let category1=e.detail.value;
        this.setData({
            category:category1
        });
    },
    publish(){
        let all_data={
            name:this.data.name,
            category:this.data.category,
            price:this.data.price,
            description:this.data.description,
            addrname:this.data.address_name,
            address:this.data.address,
            longitude:this.data.longitude,
            latitude:this.data.latitude,
            publisherid:app.globalData.userinfo.id,
            coverurl:this.data.coverUrl,
            descurl:this.data.descUrl
        };
        console.log(all_data);
        if(this.data.name==''||this.data.price==''||this.data.description==''||this.data.address=='' || this.data.category=='' ||
        this.data.address_name=='' || this.data.longitude=='' || this.data.latitude==''||this.data.publisherid=='' ||this.data.coverUrl==''||this.data.descUrl==''){
            swan.showModal({
                title: '提示',
                content: '您有信息未填',
                cancel:false
            });
            return false;
        }
        swan.request({
            url: fakedata.urls.addgood,
            header: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Developer-Server-Domain': fakedata.host,
                'X-SP-Accept-Encoding': 'base64'
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            data:{
                name:this.data.name,
                category:this.data.category,
                price:this.data.price,
                description:this.data.description,
                addrname:this.data.address_name,
                address:this.data.address,
                longitude:this.data.longitude,
                latitude:this.data.latitude,
                publisherid:app.globalData.userinfo.id,
                coverurl:fakedata.host+"/upload/"+this.data.coverUrl,
                descurl:fakedata.host+"/upload/"+this.data.descUrl
            },
            success: res => {
                console.log(res);
                if(res.statusCode==200){
                    swan.showModal({
                        title:'添加成功'
                    });
                    swan.switchTab({
                        url:"/pages/index/index"
                    });
                }
            },
            fail: err => {
                swan.showModal({
                    title: '错误信息',
                    content: JSON.stringify(err)
                });
            }
        });
    }
});